// pages/crowdFunding/detail/detail.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nowDate: '',
        userInfo: {},
        isLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            nowDate: new Date().getTime()
        })
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        let userId = wx.getStorageSync('userId')
        if (userId) {
            return {
                title: '财大师-梦想赋能',
                path: '/pages/crowdFunding/detail/detail?userShare=' + userId,
                imageUrl: 'https://www.meiboshi.vip/static/images/wx_sm/home/crowdfunding-img-1.jpg'
            }
        }
    },

    getUserInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            isStore: 1
        }
        common.post('User/user_info', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            that.setData({
                userInfo: res
            })

        }).catch(function(e) {})
    },

    goCrowdFunding: function() {
        if (this.data.isLoading) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        let data = {}
        common.post('Crowd/apply_crowd', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
            } else {
                wx.showModal({
                    title: '提示',
                    content: '成功发起众筹',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            wx.navigateTo({
                                url: '/pages/crowdFunding/apply/apply'
                            })
                        }
                    }
                })
            }
        }).catch(function(e) {})
    },

    goPay: function() {
        if (this.data.isLoading) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        wx.login({
            success(res) {
                if (res.code) {
                    let data = {
                        code: res.code,
                        pay_client: 1
                    }
                    common.post('Crowd/apply_crowd', data).then(function(res) {
                        that.setData({
                            isLoading: false
                        })
                        wx.hideLoading()
                        if(res.code == 204){
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {}
                            })
                            return
                        }
                        wx.requestPayment({
                            timeStamp: res.timeStamp,
                            nonceStr: res.nonceStr,
                            package: res.package,
                            signType: res.signType,
                            paySign: res.paySign,
                            success(res) {
                                wx.showModal({
                                    title: '提示',
                                    content: '成功发起众筹',
                                    showCancel: false,
                                    success(res) {
                                        wx.navigateTo({
                                            url: '/pages/crowdFunding/apply/apply'
                                        })
                                    }
                                })
                            },
                            fail(res) {
                                wx.showToast({
                                    title: '支付失败',
                                    icon: 'none',
                                    duration: 2000
                                })
                            }
                        })
                    }).catch(function(e) {})
                }
            }
        })
    }
})